package com.pipi.sso.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.pipi.sso.core.exception.DormServiceException;
import com.pipi.sso.core.service.AdviceService;
import com.pipi.sso.core.enums.CommonStatusEnum;
import com.pipi.sso.core.enums.DormErrorCodeEnum;
import com.pipi.sso.core.model.AdviceBO;
import com.pipi.sso.core.util.BeanUtils;
import com.pipi.sso.core.util.DateUtil;
import com.pipi.sso.dao.manager.AdviceManager;
import com.pipi.sso.dao.model.AdviceDO;
import com.pipi.sso.dao.query.AdviceQuery;
import com.pipi.sso.dao.support.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.pipi.sso.core.service.impl
 * @date: 2019/4/23 11:38 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 */
@Service
@Slf4j
public class AdviceServiceImpl implements AdviceService {

    @Resource
    private AdviceManager adviceManager;


    @Override
    public PageResult<AdviceBO> queryByPaged(AdviceBO adviceBO) {
        try {
            AdviceQuery query = buildQuery(adviceBO);
            query.setPageNo(adviceBO.getPage());
            query.setPageSize(adviceBO.getLimit());
            PageResult<AdviceDO> pageResult = adviceManager.selectByQueryWithPage(query);
            if (CollectionUtils.isEmpty(pageResult.getResult())) {
                return PageResult.createPageResult(Lists.newArrayList(), pageResult.getPageInfo());
            }
            List<AdviceBO> adviceBOS = buildBOs(pageResult.getResult());
            return PageResult.createPageResult(adviceBOS, pageResult.getPageInfo());
        } catch (Exception e) {
            log.error("查询建议失败！adviceBO is {}", JSON.toJSONString(adviceBO), e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "查询建议失败！");
        }
    }

    @Override
    public List<AdviceBO> queryNoPaged(AdviceBO adviceBO) {
        try {
            AdviceQuery query = buildQuery(adviceBO);
            List<AdviceDO> adviceDOS = adviceManager.selectByQuery(query);
            if (CollectionUtils.isEmpty(adviceDOS)) {
                return Lists.newArrayList();
            }
            return buildBOs(adviceDOS);
        } catch (Exception e) {
            log.error("查询建议失败！adviceBO is {}", JSON.toJSONString(adviceBO), e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "查询建议失败！");
        }
    }

    @Override
    public void addAdvice(AdviceBO adviceBO) {
        try {
            AdviceDO adviceDO = BeanUtils.createCloneBean(adviceBO, AdviceDO.class);
            adviceDO.setStatus(CommonStatusEnum.NO.getCode());
            adviceDO.setIsDeleted(CommonStatusEnum.NO.getCode());
            adviceManager.insertSelective(adviceDO);
        } catch (Exception e) {
            log.error("添加建议失败！adviceBO is {}", JSON.toJSONString(adviceBO), e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "添加建议失败！");
        }
    }

    @Override
    public void updateAdvice(AdviceBO adviceBO) {
        try {
            AdviceDO adviceDO = BeanUtils.createCloneBean(adviceBO, AdviceDO.class);
            adviceManager.updateByPrimaryKeySelective(adviceDO);
        } catch (Exception e) {
            log.error("修改建议失败！adviceBO is {}", JSON.toJSONString(adviceBO), e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "修改建议失败！");
        }
    }

    @Override
    public void updateStatus(AdviceBO adviceBO) {

    }

    private AdviceQuery buildQuery(AdviceBO adviceBO) {
        AdviceQuery query = new AdviceQuery();
        query.setOrderByClause("gmt_create desc");
        if (Objects.nonNull(adviceBO)) {
            AdviceQuery.Criteria criteria = query.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
            if (Objects.nonNull(adviceBO.getAdviceId())) {
                criteria.andAdviceIdEqualTo(adviceBO.getAdviceId());
            }
            if (StringUtils.isNotBlank(adviceBO.getAdviceTitle())) {
                criteria.andAdviceTitleLike("%" + adviceBO.getAdviceTitle() + "%");
            }
        }
        return query;
    }

    private List<AdviceBO> buildBOs(List<AdviceDO> adviceDOS) {
        return adviceDOS.stream().map(this::buildBO).collect(Collectors.toList());
    }

    private AdviceBO buildBO(AdviceDO adviceDO) {
        if (Objects.isNull(adviceDO)) {
            return null;
        }
        AdviceBO adviceBO = BeanUtils.createCloneBean(adviceDO, AdviceBO.class);
        adviceBO.setStatusDesc(CommonStatusEnum.getAdviceStatusByCode(adviceBO.getStatus()));
        adviceBO.setGmtCreateDesc(DateFormatUtils.format(adviceBO.getGmtCreate(), DateUtil.PATTERN_2));
        return adviceBO;
    }
}
